import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wch/utils/time_utils.dart';
import '../utils/common_utils.dart';
import '../utils/log_utils.dart';
//
//SharedPreferences 辅助类
//
class PreferencesHelper {
  static const String userTokenKey = 'userToken';
  static const String tokenTimeStampKey = 'tokenTimeStamp';
  static const String memberMobile = 'memberMobile';
  static const String memberMobileKey = 'memberMobileKey';
  static const String professionSettingKey = 'professionSettingKey';
  static const String memberId = 'memberId';
  static const String memberIdKey = 'memberIdKey';
  static const String cacheDataKey = 'cacheDataKey';
  static const String appId = 'appId';
  static const String upgradeDevice = 'upgradeDevice';
  ///首次使用,是否同意弹窗隐私政策
  static const String agreeToPrivacyKey = 'agree2PrivacyKey';
  ///医生所属科室
  static const String departmentKey = 'departmentKey';
  static const String personKey = 'personKey';

  static SharedPreferences? _prefs;

  ///需要在调用 SharedPreferences 前，调用此方法
  static Future<SharedPreferences> getPreferences() async {
    if (_prefs != null) {
      return Future.value(_prefs);
    }

    _prefs = await SharedPreferences.getInstance();
    return Future.value(_prefs);
  }

  static String? getUserToken() {
    String? token =_prefs!.getString(userTokenKey);
    LogUtils.log('token=$token');
    return token;
  }
  static String? getMemberMobile() {
    return _prefs!.getString(memberMobileKey);
  }
  static int? getMemberId() {
    return _prefs!.getInt(memberIdKey);
  }
  static int? getTokenTimeStamp() {
    return _prefs!.getInt(tokenTimeStampKey);
  }

  static void setUserToken(String userToken) async {
    _prefs!.setString(userTokenKey, userToken);
    getUserToken();

    int timeStamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    _prefs!.setInt(tokenTimeStampKey, timeStamp);
  }

  static String? getAppId() {
    LogUtils.log("-----使用appid = ${_prefs!.getString(appId)}");
    return _prefs!.getString(appId);
  }


  static void setAppId(String id) async {
    _prefs!.setString(appId, id);
    LogUtils.log("-----保存appid = $id");
  }
  static String? getUpgradeDeviceStr() {
    LogUtils.log("-----使用upgradeDevice = ${_prefs!.getString(upgradeDevice)}");
    return _prefs!.getString(upgradeDevice);
  }


  static void setUpgradeDeviceStr(String upgradeDeviceStr) async {
    _prefs!.setString(upgradeDevice, upgradeDeviceStr);
    LogUtils.log("-----保存upgradeDevice = $upgradeDevice");
  }


  static void setMemberMobile(String memberMobile) {
    _prefs!.setString(memberMobileKey, memberMobile);
  }

  static void setMemberId(int memberId) {
    _prefs!.setInt(memberIdKey, memberId);
  }

  static bool tokenIsValid() {
    int curTimeStamp = TimeUtils.getCurrentTimeStamp();
    int? tokenTimeStamp = getTokenTimeStamp();
    String? userToken = getUserToken();

    if (tokenTimeStamp == null) {
      return false;
    }

    if (!CommonUtils.checkNotNull(userToken)) {
      return false;
    }

    return curTimeStamp - tokenTimeStamp < 60*60*24*30;
  }

  static void clearToken() {
    setUserToken('');
  }

  ///睡眠报告缓存
  static void setSleepReportCache(List<String> jsonList) {
    _prefs!.setStringList(cacheDataKey, jsonList);
  }

  static List<String>? getSleepReportCache() {
    return _prefs!.getStringList(cacheDataKey);
  }

  static void setAgreePrivacy(bool isAgree) {
    _prefs!.setBool(agreeToPrivacyKey, isAgree);
  }

  static bool? getIsAgreePrivacy() {
    return _prefs!.getBool(agreeToPrivacyKey);
  }
  ///重新分期缓存
  static void setStages(String key, List<String> stages) {
    _prefs!.setStringList(key, stages);
  }

  static List<String>? getStages(String key) {
    return _prefs!.getStringList(key);
  }
  ///设置 wifi 账号和密码
  static void setWifiAndPass(String wifi, String pass) {
    _prefs!.setString(wifi, pass);
  }
  static String? getWifiPass(String wifi) {
    return _prefs!.getString(wifi);
  }
  static void setDepartment(String department) {
    _prefs!.setString(departmentKey, department);
  }
  static String? getDepartment() {
    return _prefs!.getString(departmentKey);
  }
  static void setPerson(String person) {
    _prefs!.setString(personKey, person);
  }
  static String? getPerson() {
    return _prefs!.getString(personKey);
  }
  static void setFirstLogin(String mobile) {
    _prefs!.setBool('${mobile}_login', false);
  }
  static bool? isFirstLogin(String mobile) {
    return _prefs!.getBool('${mobile}_login');
  }
}